﻿import QtQuick 2.0

Rectangle{
    property int xScaleZoom: 0
    property int yScaleZoom: 0

    Rectangle{
        id:chartView
        border.color: "red"
    }
    Rectangle{
        id: recZoom
        border.color: "steelblue"
        border.width: 1
        color: "steelblue"
        opacity: 0.3
        visible: false
        transform: Scale { origin.x: 0; origin.y: 0; xScale: xScaleZoom; yScale: yScaleZoom}
    }
    MouseArea {
        anchors.fill: parent
        hoverEnabled: true
        onPressed: {
            recZoom.x = mouseX;
            recZoom.y = mouseY;
            recZoom.visible = true;
        }
        onMouseXChanged: {
            if (mouseX - recZoom.x >= 0) {
                xScaleZoom = 1;
                recZoom.width = mouseX - recZoom.x;
            } else {
                xScaleZoom = -1;
                recZoom.width = recZoom.x - mouseX;
            }
        }
        onMouseYChanged: {
            if (mouseY - recZoom.y >= 0) {
                yScaleZoom = 1;
                recZoom.height = mouseY - recZoom.y;
            } else {
                yScaleZoom = -1;
                recZoom.height = recZoom.y - mouseY;
            }
        }
        onReleased: {
            var x = (mouseX >= recZoom.x) ? recZoom.x : mouseX
            var y = (mouseY >= recZoom.y) ? recZoom.y : mouseY
            var rect = (Qt.rect(x, y, recZoom.width, recZoom.height));
            chartView.x = rect.x
            chartView.y = rect.y;
            chartView.width = rect.width
            chartView.height = rect.height
            recZoom.visible = false;
        }
    }
}
